package com.myebills.ebpsfrontend.cust.maxis;

import com.myebills.ebpsfrontend.cust.controller.*;
import com.myebills.ebpsfrontend.cust.utils.*;
import com.checkfree.isolutions.customer.persist.*;
import com.checkfree.isolutions.admin.ObjectId;
import com.checkfree.isolutions.statement.StatementSection;
import com.checkfree.isolutions.statement.persist.*;
import com.checkfree.isolutions.utility.CFiError;
import java.util.*;
import java.text.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.ibm.logging.ILogger;
import com.ibm.logging.IRecordType;

/**
 * Module Name  : CusBillListServlet_v1r00
 * Description  : Bill Listing for customer
 * Last Updated : 2002-10-10
 *
 * @author lhlue
 *
 * Remarks :
 *
 */

public class CompareBillServlet_v1r00 extends EbppxTransactionServlet {
    //retrieve previous 6 months statements
    ILogger trcLogger = null;

    public void doTransaction(
    HttpServletRequest req,
    HttpServletResponse res)
    throws ServletException {

        final String fName = "doTransaction";
        trcLogger = getTraceLogger(req);

        if (trcLogger.isLogging())
            trcLogger.entry(IRecordType.TYPE_INFO, this, fName);

        try {
            final HttpSession session = req.getSession(false);

            if (null == session)
                throw new IllegalStateException("no session established");

		    final String randomKey = (String) session.getAttribute(EbppxConstants.RANDOM_KEY);

            //Get attribute from session for the logon user
            final EbppxEcsUser ecsUser = (EbppxEcsUser) session.getAttribute(EbppxConstants.ECS_USER);

            EbppxUrlManager urlManager = getUrlManager(req);
            // get the account no selected in JSP
            String strAccNo = urlManager.getRequestParameter("accNo");
            //strAccNo = EncryptionHelper.decrypt(strAccNo,randomKey);
            int strComp = Integer.parseInt(urlManager.getRequestParameter("comp"));
			System.out.println("compcompcompcompcompcompcompcompcompcompcompcompcompcompcomp"+urlManager.getRequestParameter("comp"));
            String strRedirect = urlManager.getRequestParameter("redirect");

			String simNo = "";
			simNo = urlManager.getRequestParameter("simno");
			if(!simNo.equals(""))
				ecsUser.setSimNo(simNo);
			
            //get the vector of Accounts
            final Vector cfiAccounts = ecsUser.getAccounts();
            final Vector cfiAcctExtKeys = ecsUser.getAccountExternalKeys();
            Vector cfiStatements = new Vector();

//poon test start
            final Vector cfiOldAccounts = ecsUser.getOldAccounts();
            req.setAttribute("oldAccounts",cfiOldAccounts);

			//to get old acc no for selected new acc no
			String oldAccountNo = ecsUser.getOldAccount(cfiAcctExtKeys, cfiOldAccounts, strAccNo);
//poon test end
System.out.println("here1!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
			//to check is there any old account number - start
			int oldAccountDisplayCount = 0;
			String oldAccountDisplay = "N";
			String accountId = "";
			String statementId = "";
			//for (java.util.Enumeration enumAccounts = cfiAcctExtKeys.elements(); enumAccounts.hasMoreElements();) {
			String accno = ecsUser.getSAccount();
			if(accno != null){
				//final String tAccountNo = (String) enumAccounts.nextElement();

                Merchant cfiMerchant = MerchantHomeImpl.singleton().findByName(ecsUser.getMerchantName());
                Account account = AccountHomeImpl.singleton().findByExternalKeyAndMerchant(accno, cfiMerchant);
                accountId = account.getId().toString(true);
                List statementList = new ArrayList();
                for(int cIndex = 0; cIndex < 2; cIndex++){
                	if(cIndex == 0){
		                Statement compStatement0 =  StatementUtil.getStatement(account,EbppxCusConstants.STMT_START_MONTH,0);
		                statementList.add(compStatement0);
		                if(compStatement0 != null){
		                	statementId = compStatement0.getId().toString(true);
			        		StatementSection cfiRootSection = compStatement0.getRootSection();
			        		
							String param0 = "?statementId=" + EncryptionHelper.encodeURL(EncryptionHelper.encrypt(statementId,randomKey)) + "&accountId=" + EncryptionHelper.encodeURL(EncryptionHelper.encrypt(accountId,randomKey))+"&accNo="+accno;
							session.setAttribute("statementId0", param0);
		                }
	                }else if(cIndex == 1){
	                	Statement compStatement1 =  StatementUtil.getStatement(account,EbppxCusConstants.STMT_START_MONTH,strComp);
						if(compStatement1 != null){
							statementId = compStatement1.getId().toString(true);
							String param1 = "?statementId=" + EncryptionHelper.encodeURL(EncryptionHelper.encrypt(statementId,randomKey)) + "&accountId=" + EncryptionHelper.encodeURL(EncryptionHelper.encrypt(accountId,randomKey))+"&accNo="+accno;
							session.setAttribute("statementId1", param1);
						}
	                	statementList.add(compStatement1);
	                }
                }
				System.out.println("here2!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
					cfiStatements = StatementUtil.getStatements(account, EbppxCusConstants.STMT_START_MONTH);
	        		if(cfiStatements != null){
	        			String statementsCount = Integer.toString(cfiStatements.size());
	        			session.setAttribute("statementsCount", statementsCount);
	        			
	        			List stmtDateList = new ArrayList();
	        			for(int cficounts = 0; cficounts < cfiStatements.size(); cficounts++){
	                        final Date dtVal = ((Statement)cfiStatements.get(cficounts)).getDate();
	                        final DateFormat dfdisp = new SimpleDateFormat("dd MMM yyyy");
	                        String stdate = EbppxUtil.htmlEncode(dfdisp.format(dtVal));
	        				stmtDateList.add(stdate);
	        			}
						System.out.println("here3!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
	        			session.setAttribute("stmtDateList", stmtDateList);
	        		}
				
                //cfiStatements.addElement(cfiStatement);
                req.setAttribute("compareStmtList", statementList);
				//String stmtIndex = EncryptionHelper.encodeURL(EncryptionHelper.encrypt(Integer.toString(0),randomKey));
                
                //session.setAttribute("stmtIndex", stmtIndex);
			}
			/*if (oldAccountDisplayCount > 0) {
				oldAccountDisplay = "Y";
			}*/
            req.setAttribute("oldAccountDisplay",oldAccountDisplay);
			//to check is there any old account number - end
            req.setAttribute("comp", Integer.toString(strComp));
            req.setAttribute(EbppxConstants.CFI_ACCOUNTS, cfiAccounts);
            req.setAttribute("acctExtKeys",cfiAcctExtKeys);

            //if an account has been selected then get the statements
            if (strAccNo !=null && !strAccNo.equals("")) {
                if (trcLogger.isLogging())
                    trcLogger.text(IRecordType.TYPE_INFO, this, fName,"requested acc no: "+strAccNo);

                req.setAttribute("accNo", strAccNo);
                req.setAttribute("redirect", strRedirect);
            }

            session.setAttribute("fromPage","0");
			session.setAttribute(EbppxConstants.ECS_USER, ecsUser);
			req.setAttribute("forwardFrom",req.getRequestURI());
            forwardToMappedUrl(req, res, EbppxConstants.URL_JSP_PARAM+"_"+ecsUser.getMSegment());
        }
        catch (Throwable t) {
			System.out.println(t.toString());
            throw new ServletException(t.toString(), t);
        }
        finally {
            if (trcLogger.isLogging())
                trcLogger.exit(IRecordType.TYPE_INFO, this, fName);
        }

    }

}
